package com.zy.socket.exampls;

import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * @Author Gong
 * @Description 1.客户端发送内容给服务端，服务端将内容打印到控制台上。
 * @dATE 2021/12/19 10:41
 **/
public class Server1 {
    public static void main(String[] args) {
        InputStream inputStream = null;
        Socket accept =null;
        ServerSocket serverSocket =null;
        try {
             serverSocket = new ServerSocket(8989);
             accept = serverSocket.accept();
            //获取流进行操作
             inputStream = accept.getInputStream();
            byte[] b = new byte[1024];
            int len;
            while ((len=inputStream.read(b))!=-1){
                String str = new String(b,0,len);
                System.out.println(str);
            }

        } catch (IOException e) {
            e.printStackTrace();
        }finally{
            if(inputStream!=null){
                try {
                    inputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(accept!=null){
                try {
                    accept.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(serverSocket!=null){
                try {
                    serverSocket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }


    }
}
